package com.example.intenttrial;

import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class RequestActivity extends Activity {
	
	String thumbNailUrl;
	TextView tv;
	Button btn;
	boolean flag = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.screen_request);
		
		tv = (TextView) findViewById(R.id.tv_result);
		btn = (Button) findViewById(R.id.btn_send);
		
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Thread t = new Thread(new Runnable() {
					
					@Override
					public void run() {
						flag = true;
					}
				});
				t.start();
			}
		});
		thumbNailUrl = getVimeoThumbNailURL("36820781");
		tv.setText(thumbNailUrl);
	}

	private String getVimeoThumbNailURL(String imageId) {
		String requestUrl = "http://vimeo.com/api/v2/video/" + imageId + ".xml";
		URL url = null;
		URLConnection conn = null;
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		Document doc = null;
		
		String thumbNailUrl = "not found";
		
		try {
			url = new URL(requestUrl);
			conn = url.openConnection();
			builder = factory.newDocumentBuilder();
			doc = builder.parse(conn.getInputStream());
			NodeList listOfVideos = doc.getElementsByTagName("thumbnail_medium");
//			NodeList listOfVideos = doc.getElementsByTagName("thumbnail_small");
//			NodeList listOfVideos = doc.getElementsByTagName("thumbnail_large");

			thumbNailUrl = listOfVideos.item(0).getTextContent();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("thumbNailUrl " + thumbNailUrl);
		
		return thumbNailUrl;
    }
	
}
